import {
  getInfoByIdApi
} from "../../../https/user"
import {
  getAllDynamicApi,
  deleteDynamicApi,
  getAllDynamicMyApi,
  postSwitchApi
} from '../../../https/dynamic'
import {
  BASE_URL
} from '../../../constant/index'

import {
  getIsFollowingApi,
  postFollowApi,
  postUnFollowApi
} from '../../../https/user'

// pages/my/detail/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    BASE_URL,
    // 顶部标题栏动态设置
    topBarInfo: {
      navBarHeight: app.globalData.navBarHeight,
      menuRight: app.globalData.menuRight,
      menuTop: app.globalData.menuTop,
      menuHeight: app.globalData.menuHeight,
    },
    current: 0,
    info: {},
    dynamicList: {},
    limit: 200,
    page: 1,
    total: 100,
    isFollowing: false,
    showMore: false,
    changeDocid: "", // 举报相关
    changeUserid: "",
    changeIsShow: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let docid = options.docid
    this.setData({
      'info.id': docid
    })
    console.log(docid)
    if (!docid) {
      docid = wx.getStorageSync('user').id
    }
    console.log(docid)

  },
  async onShow() {
    let id = this.data.info.id
    if (!id) {
      const userData = wx.getStorageSync('userData')
      id = userData.id
    }
    await this.getUserInfo(id)
    this.getDynamicList()
    this.getIsFollowing(id)
  },
  // 获取是否关注用户
  async getIsFollowing(userId) {
    const getRes = await getIsFollowingApi({
      id: userId
    })
    if (getRes.code == 200) {
      console.log(getRes.data)
      this.setData({
        isFollowing: getRes.data
      })
    }
  },
  // 切换关注状态
  async toggleFollow() {
    console.log(this.data.info.id)
    const id = this.data.info.id
    const isFollowing = this.data.isFollowing
    if (isFollowing) {
      const postRes = await postUnFollowApi({
        id
      })
    } else {
      const postRes = await postFollowApi({
        id
      })
      if (postRes.code == 200) {
        wx.showToast({
          title: '关注成功',
          icon: 'success'
        })
      }

    }
    this.getIsFollowing(id)
    await this.getUserInfo(id)

  },
  // 获取动态列表
  async getDynamicList() {
    const userId = this.data.info.id
    console.log(userId)
    const {
      total,
      page,
      limit
    } = this.data
    const isMy = this.data.isMy
    console.log('isMy: ' + isMy)
    let getRes
    if (isMy) {
      getRes = await getAllDynamicMyApi({
        page,
        limit
      })
    } else {
      getRes = await getAllDynamicApi({
        page,
        limit,
        userId
      })
    }

    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        dynamicList: getRes.data.list,
        limit: getRes.data.limit,
        // page: getRes.data.page + 1,
        total: getRes.data.total
      })
    }
  },
  // 获取用户信息
  async getUserInfo(docid) {
    const getRes = await getInfoByIdApi({
      id: docid
    })
    if (getRes.code == 200) {
      const userData = wx.getStorageSync('userData') || {}
      console.log(userData.id == getRes.data.id, )
      this.setData({
        isMy: userData.id == getRes.data.id,
        info: getRes.data
      })
    }
  },
  // 切换状态
  toggleCurrent(e) {
    const current = e.currentTarget.dataset.curren
    this.setData({
      current
    })
  },
  // 打开更多
  openMore(e) {
    const docid = e.currentTarget.dataset.docid
    const userid = e.currentTarget.dataset.userid
    const isshow = e.currentTarget.dataset.isshow
    console.log(e)
    console.log(isshow)
    console.log(e)
    this.setData({
      showMore: true,
      changeDocid: docid,
      changeUserid: userid,
      changeIsShow: isshow
    })
  },
  // 关闭弹框
  closePopup() {
    this.setData({
      showMore: false,
    })
  },
  // 跳转编辑
  gotoEdit() {
    wx.navigateTo({
      url: '../edit/index',
    })
  },
  // 删除已发布的动态
  async binddelete() {
    console.log(this.data)
    const id = this.data.changeDocid
    const userid = this.data.changeUserid
    if (!id) {
      console.log('无id传入')
      return
    }
    console.log(id)
    console.log(userid)

    const user = wx.getStorageSync('userData')
    console.log(user)
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      this.setData({
        showMore: false
      })
      return
    }
    if (userid != user.id) return
    // 提示是否删除
    const {
      confirm
    } = await wx.showModal({
      title: '提示',
      content: '是否删除该动态？',

    })
    if (!confirm) return
    wx.showLoading()
    const delRes = await deleteDynamicApi({
      id
    })
    console.log(delRes)
    if (delRes.code == 200) {
      // 重新获取数据
      await this.getDynamicList()
      this.setData({
        showMore: false
      })
      wx.showToast({
        title: '删除成功',
        icon: 'none'
      })
    } else {
      wx.showToast({
        title: '删除失败',
        icon: 'error'
      })
    }
    wx.hideLoading()

  },
  // 切换显示状态
  async toggleisShow() {
    console.log(this.data)
    const id = this.data.changeDocid
    wx.showLoading()
    const postRes = await postSwitchApi({
      id
    })
    console.log(postRes)
    if (postRes.code == 200) {
      await this.getDynamicList()
      this.setData({
        showMore: false
      })
    } else {
      wx.showToast({
        title: '切换失败',
        icon: 'error'
      })
    }
    wx.hideLoading()

  },
  // 跳转详情页面
  gotoDetail(e) {
    const docid = e.currentTarget.dataset.docid
    wx.navigateTo({
      url: '../../home/detail/index?docid=' + docid,
    })
  },
  // 跳转朋友页面
  gotoFriend() {
    const info = this.data.info
    console.log(info)
    if (info.isMy) {
      wx.navigateTo({
        url: '../friend/index',
      })
    }

  },
  // 跳转聊天页面
  gotoChat(e) {
    const docid = e.currentTarget.dataset.docid
    wx.navigateTo({
      url: '../../message/chat/index?docid=' + docid,
    })
  },
  // 跳转举报界面
  gotoReport(e) {

    const docid = this.data.changeDocid

    const user = wx.getStorageSync('userData')
    console.log(user)
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      this.setData({
        showMore: false
      })
      return
    }
    if (!docid) {
      console.log("没有传入id")
      return
    }

    wx.navigateTo({
      url: '../../my/report/index?docid=' + docid,
    })
    this.setData({
      showMore: false
    })
  },
  back() {
    wx.navigateBack()
  }

})